package mn.slide.labmed.database;

import java.sql.SQLException;

import mn.slide.labmed.model.HistoryModel;
import mn.slide.labmed.model.MerkModel;
import mn.slide.labmed.model.Model;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper
{
	private static final String	DATABASE_NAME		= "Labmed.sqlite";
	private static final int	DATABASE_VERSION	= 2;

	public DatabaseHelper( final Context context )
	{
		super(context , DATABASE_NAME , null , DATABASE_VERSION);
	}

	private Dao<Model, Integer>	modelDao	= null;
	private Dao<HistoryModel, Integer>	historyDao	= null;
	private Dao<MerkModel, Integer>	merkDao	= null;
	@Override
	public void onCreate( final SQLiteDatabase db , final ConnectionSource connectionSource )
	{
		try
		{
			Log.i(DatabaseHelper.class.getName(), "onCreate");
			TableUtils.createTableIfNotExists(connectionSource, Model.class);
			TableUtils.createTableIfNotExists(connectionSource, HistoryModel.class);
			TableUtils.createTableIfNotExists(connectionSource, MerkModel.class);

		}
		catch (final SQLException e)
		{
			Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
			throw new RuntimeException(e);
		}
	}

	@Override
	public void onUpgrade( final SQLiteDatabase db , final ConnectionSource connectionSource , final int oldVersion , final int newVersion )
	{
		try
		{
			Log.i(DatabaseHelper.class.getName(), "onUpgrade");
			TableUtils.dropTable(connectionSource, Model.class, true);
			TableUtils.dropTable(connectionSource, HistoryModel.class, true);
			TableUtils.dropTable(connectionSource, MerkModel.class, true);
			onCreate(db, connectionSource);
		}
		catch (final SQLException e)
		{
			Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
			throw new RuntimeException(e);
		}
	}

	public void upgrade()
	{
		try
		{
			Log.i(DatabaseHelper.class.getName(), "onUpgrade");
			TableUtils.dropTable(connectionSource, Model.class, true);
			TableUtils.dropTable(connectionSource, HistoryModel.class, true);
			TableUtils.dropTable(connectionSource, MerkModel.class, true);
		}
		catch (final SQLException e)
		{
			Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
			throw new RuntimeException(e);
		}
	}

	public Dao<Model, Integer> getModelDao() throws SQLException
	{
		if (this.modelDao == null)
		{
			this.modelDao = getDao(Model.class);
		}
		return this.modelDao;
	}
	public Dao<HistoryModel, Integer> getHistoryDao() throws SQLException
	{
		if (this.historyDao == null)
		{
			this.historyDao = getDao(HistoryModel.class);
		}
		return this.historyDao;
	}
	public Dao<MerkModel, Integer> getMerkDao() throws SQLException
	{
		if (this.merkDao == null)
		{
			this.merkDao = getDao(MerkModel.class);
		}
		return this.merkDao;
	}
	@Override
	public void close()
	{
		super.close();
		this.modelDao = null;
	}

}
